////<!-- 预订参数 -->
import api from '../../../fetch/api'
import {
	getTreeChildNodes
} from '../../../assets/js/help.js'

export default {
	data() {
		return {
			delId: 0,
			isModify: false, // 新建/修改预订参数状态, 默认false为新建
			bookingRuleVo: {
				"id": 0,
				"parkId": 0,
				"parkName": ''
			},
			vipTypeList: [],
			parkingLotList: [],
			parkId_oldVal: '', //单选停车场-oldVal
		}
	},
	created() {

	},
	watch: {
    "bookingRuleVo.parkId":{
      handler(curVal,oldVal){
        this.parkId_oldVal = oldVal
      }
    }
  },
	methods: {
		getList() {
			let params = $.extend({}, this.searchParams, this.otherParams);
			this.getItemList(params);
		},
		//获取列表
		getItemList(aparams) {
			let self = this,
				params = {
					page: self.pageNumber,
					pageSize: self.pageSize
				};
			params = $.extend({}, params, aparams);
			api.fetchGet('park/booking/rule/list.do', params)
				.then(res => {
					let data = res.data;
					self.tbodysInfo = data.rows;
					self.total = data.total;
				}, err => {});
		},
		showDeleteRule(id) { // 删除询问
			this.delId = id;
			this.toOpenModal("delConfirmTipModal");
		},
		submitBookingRuleForm() { // 点击新增/修改参数的确认按钮
			let self = this;
			this.$refs['bookingRuleForm'].validate((valid) => {
				if (valid) { // 通过验证
					var bookingRuleForm = self.convertToBookingRuleForm(self.bookingRuleVo);
					if (bookingRuleForm.id == 0) {
						// 新增
						api.fetchPost('park/booking/rule/create.do', bookingRuleForm)
							.then(res => {
								if (res.status == 1) {
									self.showToast('保存成功', 1);
									self.getList();
									self.closed('bookingRuleModal');
								}
							}, err => {});
					} else {
						// 更新
						api.fetchPost('park/booking/rule/update.do', bookingRuleForm)
							.then(res => {
								if (res.status == 1) {
									self.showToast('保存成功', 1);
									self.getList();
									self.closed('bookingRuleModal');
								}
							}, err => {});
					}
				} else {
					return false;
				}
			});
		},
		convertToBookingRuleForm(vo) {
			var bookingRuleForm = {
				"id": 0,
				"parkId": 0,
				"name": "",
				"availableCount": "",
				"serviceFee": "",
				"enablePreAmount": false,
				"cancelMinute": "",
				"enableRefundServiceFee": false,
				"enableRefundPreAmount": false,
				"bookingType": 0,
				"step": 1,
				"vipTypeId": ""
			};
			for (var key in vo) {
				if (bookingRuleForm.hasOwnProperty(key)) {
					bookingRuleForm[key] = vo[key];
				}
			}
			return bookingRuleForm;
		},
		createRule() {
			this.bookingRuleVo = {
				"id": 0,
				"parkId": "",
				"parkName": "",
				"name": "",
				"availableCount": "",
				"serviceFee": "",
				"enablePreAmount": false,
				"cancelMinute": "",
				"enableRefundServiceFee": false,
				"enableRefundPreAmount": false,
				"bookingType": 0,
				"step": 1,
				"vipTypeId": ""
			};
			this.isModify = false;
			this.vipTypeList = [];
			this.initParkTree()
			this.toOpenModal('bookingRuleModal');
		},
		updateRule(id) {
			let self = this;
			api.fetchGet('park/booking/rule/getById.do', {
					id: id
				})
				.then(async res => {
					if (res.status == 1) {
						self.bookingRuleVo = res.data;
						self.bookingRuleVo.parkId += ''
						self.isModify = true;
						self.getVipTypeList(res.data.parkId);
						self.initParkTree()
						self.toOpenModal('bookingRuleModal');
					} else {
						self.showToast(res.message, 2);
					}
				}, err => {});
		},
		deleteRule() {
			let self = this;
			api.fetchGet('park/booking/rule/delete.do', {
					id: self.delId
				})
				.then(res => {
					if (res.status == 1) {
						self.getList();
						self.closed('delConfirmTipModal', 1);
						self.showToast('删除成功', 1);
					} else {
						self.showToast('删除失败', 2);
					}
				}, err => {});
		},
		getVipTypeList(parkId) {
			let self = this;
			self.vipTypeList = [];
			api.fetchGet('vip/vipType/booking/list.do', {
					parkId: parkId
				})
				.then(res => {
					self.vipTypeList = res.data;
				}, err => {});
		},
		initParkTree() {
			var _self = this;
			API.fetchGet('/park/booking/getParkingBaseDataTree.do', {})
				.then(res => {
					if (res.data) {
						let nodes = res.data;
						if (nodes.childrenNum && nodes.children) {
							_self.parkingLotList = getTreeChildNodes(nodes.children, '')
							// _self.bookingRuleVo.parkId = _self.parkingLotList[0].value
							// _self.bookingRuleVo.parkName = _self.parkingLotList[0].name
						}
					} else {
						_self.parkingLotList = []
					}
				}, err => {});
		},
		async selectParkSure(itemId) {
			let self = this;
			let pItem = self.parkingLotList.find(elem => {
				return elem.value == itemId
			})
			let parkId = pItem.value;
			let parkName = pItem.name;
			// 检查是否已有该停车场的预订参数, 不能重复
			await API.fetchGet('park/booking/rule/park/check.do', {
					parkId: itemId
				})
				.then(res => {
					if (res.status == 1) {
						if (res.data) {
							self.bookingRuleVo.parkId = self.parkId_oldVal
							self.showToast('停车场[' + parkName + ']已存在记录, 请重新选择', 0, 2000);
							return;
						}
						self.getVipTypeList(parkId);
						self.bookingRuleVo.vipTypeId = '';
						self.bookingRuleVo.parkId = parkId;
						self.bookingRuleVo.parkName = parkName;
					}
				}, err => {});
		}
	}
}